package com.licheng.service.imp;

import com.licheng.mapper.LoginMapper;
import com.licheng.pojo.Emp;
import com.licheng.pojo.LoginInfo;
import com.licheng.service.LoginService;
import com.licheng.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @Override
    public LoginInfo login(Emp emp) {
        LoginInfo lgif = loginMapper.login(emp);
        if(lgif != null) {
            //登录成功
            Map<String, Object> ojm = new HashMap<>();
            ojm.put("id", lgif.getId());
            ojm.put("username", lgif.getUsername());
            ojm.put("name", lgif.getName());
            String jwt = JwtUtils.generateJwt(ojm);
            lgif.setToken(jwt);
            return lgif;
        } else return null;
    }
}
